<template>
	<div class="system-dept-container layout-pd">
		<el-card shadow="hover">
			<div class="system-dept-search mb15">
				<el-button size="default" type="success" class="ml10" @click="onOpenAdd(state.add)">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增部门
				</el-button>
			</div>
			<el-table
				:data="state.tableData.data"
				v-loading="state.tableData.loading"
				style="width: 100%"
				row-key="id"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column label="名称" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.name }}</span>
					</template>
				</el-table-column>
				<el-table-column label="联系电话" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.phone }}</span>
					</template>
				</el-table-column>
				<el-table-column label="邮箱" show-overflow-tooltip width="80">
					<template #default="scope">
						<span>{{ scope.row.email }}</span>
					</template>
				</el-table-column>
				<el-table-column label="排序" show-overflow-tooltip width="80">
					<template #default="scope">
						{{ scope.row.sort }}
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenAdd(state.add)">新增</el-button>
						<el-button size="small" text type="primary" @click="onOpenEdit(state.edit, scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<DeptDialog ref="deptDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemDept">
import { defineAsyncComponent, ref, onMounted, reactive } from 'vue';
import { RouteRecordRaw } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import { deptApi } from '/@/api/system/dept/index';
import { setBackEndControlRefreshRoutes } from "/@/router/backEnd";
import constConfig from "/@/utils/constConfig"

// 引入组件
const DeptDialog = defineAsyncComponent(() => import('/@/views/system/dept/dialog.vue'));

// 定义变量内容
const deptDialogRef = ref();
const state = reactive({
	tableData: {
		data: [] as RouteRecordRaw[],
		loading: true,
	},
	formData: {
		id: "",
		name: "",
		parentId: "0",
		phone: "",
		email: "",
		sort: 100,
		status: "0",
	},
	...constConfig.dialogType
});

// 获取路由数据，真实请从接口获取
const getTableData = () => {
	state.tableData.loading = true;
	deptApi().tree().then(res=>{
		state.tableData.data = res.data;
		state.tableData.loading = false;
	})
};
// 打开新增部门弹窗
const onOpenAdd = (type: string) => {
	let row = JSON.parse(JSON.stringify(state.formData));
	deptDialogRef.value.openDialog(type, row);
};
// 打开编辑部门弹窗
const onOpenEdit = (type: string, row: DeptType) => {
	row = JSON.parse(JSON.stringify(row))
	deptDialogRef.value.openDialog(type, row);
};
// 删除当前行
const onTabelRowDel = (row: DeptType) => {
	ElMessageBox.confirm(`${constConfig.delString.tip1 + row.name + constConfig.delString.tip2}`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		deptApi().del(row.id).then(() => {
			getTableData();
			setBackEndControlRefreshRoutes() // 刷新部门，未进行后端接口测试
		})
			
	}).catch(() => {});
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
